(function (mui, window) {
    var v2PortalApi = window.v2PortalApi = {};
    var baseRequest = window.baseRequest;
    var appConfig = window.appConfig;
    var commonTool = window.commonTool;
    var locale = window.dataManager.getLocale();

    var getShopList = appConfig.v2ApiPortalUrl + '/getPlaceInfoListPage?';
    //获取场所信息
    v2PortalApi.getShopListOfUser = function (userName, queryCondition, start, size, callback) {
        baseRequest.digestGet(getShopList + mui.param({
                queryCondition: queryCondition,
                start: start,
                size: size,
                user_name: userName
            }), 15000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.code === 0) {
                callback(0, data.data);
            } else {
                callback(1, data.message);
            }
        });
    }
	// add by ding
	//获取场所下所有设备
	//newV2CloudUrl + "/shopdevicesGetApp/" + shopId + "?user_name=" + username,
	v2PortalApi.getDevicesOfShop = function(userName, shopId,callback){
		var getDevicesOfShopUrl = appConfig.v2ApiPortalUrl + "/shopdevicesGetApp/"+shopId+"?user_name="+userName;
		baseRequest.digestGet(getDevicesOfShopUrl, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.code === 0) {
                    callback(0,data.data);
                } else {
                    callback(1, data.message);
                }
            }
        });
	}
	
    //添加设备
    var addDeviceUrl = appConfig.v2ApiPortalUrl + "/shopdevicesApp";
    v2PortalApi.addDevice = function (deviceInfo,callback) {
        //return baseRequest.digestPost(addDeviceUrl, deviceInfo, 5000);
        baseRequest.digestPost(addDeviceUrl, deviceInfo, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.code === 0) {
                callback(0, data.data);
            } else {
                callback(1, data.message);
            }
        });
    }

    //获取场所设备所有分组
    var getGroupInfoUrl = appConfig.v2ApiPortalUrl + "/getdevicegroupbyshopid?";
    v2PortalApi.getGroupInfo = function (userName, nasId, callback) {
   	 	baseRequest.digestGet(getGroupInfoUrl + mui.param({
   	 		 user_name: userName,
   	 		 shop_id: nasId
   	 	}), function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.code === 0) {
                    callback(0,data.data);
                } else {
                    callback(1, data.message);
                }
            }
        });
    }
    
    //删除设备
    v2PortalApi.deleteDevice = function(userName,deviceId,callback){
    		var deleteDeviceUrl = appConfig.v2ApiPortalUrl + "/deviceDeleteApp/" + deviceId + "?user_name=" + userName;
	    baseRequest.digestDelete(deleteDeviceUrl, function(code, data) {
		    	if(code === 2 || code === 1) {
		    		return callback(2);
		    	}
		    	if(code === 0) {
		    		if(data.code === 0) {
		    			callback(0);
		    		} else {
		    			callback(1);
		    		}
		    	}
	    });
    }
    
    //删除场所
    v2PortalApi.deleteShopByShopId = function (userName,shopId,callback){
    		var deleteShopUrl = appConfig.v2ApiPortalUrl + "/shopDeleteApp/" + shopId + "?user_name=" + userName;
    		baseRequest.digestDelete(deleteShopUrl, function(code, data) {
		    	if(code === 2 || code === 1) {
		    		return callback(2);
		    	}
		    	if(code === 0) {
		    		if(data.code === 0) {
		    			callback(0);
		    		} else {
		    			callback(1,data.message);
		    		}
		    	}
	    });
    }
    
    // end
    //获取场景分类
    var getSceneClassUrl = appConfig.v2ApiPortalUrl + '/getAllScenario';
    v2PortalApi.getSceneClass = function (callback) {
        baseRequest.digestGet(getSceneClassUrl + mui.param({}), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.code === 0) {
                	callback(0, data.data);
                } else {
                    callback(1);
                }
            }
        });
    }
    //获取场景所属区域
    var getRegionUrl = appConfig.v2ApiPortalUrl + '/regions?';
    v2PortalApi.getRegion = function (userName, callback) {
        baseRequest.digestGet(getRegionUrl + mui.param({
                user_name: userName
            }), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.code === 0) {
                	callback(0, data.data);
                } else {
                    callback(1);
                }
            }
        });
    }
    //获取场景所属行业
    var getIndustryUrl = appConfig.v2ApiPortalUrl + '/getAllIndustryName?';
    v2PortalApi.getIndustry = function (userName, callback) {
        baseRequest.digestGet(getIndustryUrl + mui.param({
                user_name: userName
            }), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.code === 0) {
                	callback(0, data.data);
                } else {
                    callback(1);
                }
            }
        });
    }
    //添加场所
    var addPlaceUrl = appConfig.v2ApiPortalUrl + '/addShopApp';
    v2PortalApi.addPlace = function (placeInfo, callback) {
        baseRequest.digestPost(addPlaceUrl, placeInfo, 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.code === 0) {
                callback(0, data.data);
            } else {
                callback(1, data.message);
            }
        });
    }
    
    //验证注册手机号是否存在
    var isPhoneNumExistUrl = appConfig.v2ApiPortalUrl + '/isExistPhone/';
    v2PortalApi.isPhoneNumExist = function(phoneNum,callback){
    	baseRequest.digestGet(isPhoneNumExistUrl+phoneNum, 15000, function (code, data) {
    		console.log(JSON.stringify(data));
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (code === 0) {
                if (data.errcode) {
                    callback(1);
                } else {
                    callback(0, data.data);
                }
            }
        });
    }
    //验证注册邮箱是否存在
    var isEmailUserExistUrl = appConfig.v2ApiPortalUrl + '/sso/isExistedUser';
    v2PortalApi.isEmailExist = function(userEmail,callback){
    	 baseRequest.digestPost(isEmailUserExistUrl, JSON.stringify({user_email:userEmail}), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.ret_code === 0) {
                callback(0);
            } else {
                callback(1, data.ret_code);
            }
        });
    }
    
    //用户注册
    var userRegistUrl = appConfig.v2ApiPortalUrl + '/registUser';
    v2PortalApi.phoneUserRegist = function (phoneUserInfo, callback) {
        baseRequest.digestPost(userRegistUrl, JSON.stringify(phoneUserInfo), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.error_code === 0) {
                callback(0);
            } else {
                callback(1, data);
            }
        });
    }
    v2PortalApi.emailUserRegist = function (emailUserInfo, callback) {
        baseRequest.digestPost(userRegistUrl, JSON.stringify(emailUserInfo), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.error_code === 0) {
                callback(0);
            } else {
                callback(1, data);
            }
        });
    }
    
    //手机找回密码
    var resetPasswordUrl = appConfig.v2ApiPortalUrl + '/resetPwd';
    v2PortalApi.resetPwd = function (resetInfo, callback) {
        baseRequest.digestPost(resetPasswordUrl,JSON.stringify(resetInfo), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.error_code === 0) {
                callback(0);
            } else {
                callback(1, data.error_message);
            }
        });
    }
    
    //邮箱找回密码
    var resetPwdByEmailUrl = appConfig.v2ApiPortalUrl + '/sso/resetPwdWithEmail';
    v2PortalApi.resetPwdByEmail = function (emailReset, callback) {
        baseRequest.digestPost(resetPwdByEmailUrl,JSON.stringify(emailReset), 5000, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (data.error_code === 0) {
                callback(0);
            } else {
                callback(1, data.error_message);
            }
        });
    }
    
    //add by zhangfuqiang
    /**
     * 获取所有的设备信息
     * @type {string}
     */
    var deviceInfoUrl = appConfig.v2ApiPortalUrl + '/getDeviceInfo';
    v2PortalApi.getDeviceInfo = function (userName,callback) {
        baseRequest.digestPost(deviceInfoUrl, {
            tenant_name: userName
        }, function (code, data) {
            if (code === 1 || code === 2) {
                return callback(2);
            }
            if (!data.dev_list) {
                return callback(1,locale.base.err.request); // TODO 获取设备信息失败
            }
            callback(0, data.dev_list);
        });
    };
    /**
     * 获取设备版本信息
     * @type {string}
     */
    var modelVersionUrl = appConfig.v2ApiPortalUrl + '/getModelVersion?model=';
    v2PortalApi.getModelVersion = function (model, callback) {
        baseRequest.digestGet(modelVersionUrl + model, function (code, data) {
            if (code === 2 || code === 1) {
                return callback(2);
            }
            if (Number(data.error_code) === 1) {
                return callback(1, data.error_message);
            }
            callback(0, data.data_list);
        });
    }
    //end
}(mui, window));
